clear all
set more off 

cd "C:\Dropbox\Average_target_survey\replication_folder\dofiles"
quiet do load_rreg2


cd "C:\Dropbox\Average_target_survey\replication_folder\workfiles"
use step001, clear

*** ATTENTION: comment this for the FULL SAMPLE

*** winsorize/drop unrealistic observations
winsor2 edy_12m, cuts(2 98) suffix(_w)
winsor2 edy_5y, cuts(2 98) suffix(_w)

*** interaction of priors and posteriors
gen edy_12m_w_X_IT=edy_12m_w*treatment_IT
gen edy_12m_w_X_AIT=edy_12m_w*treatment_AIT

*** winsorize/drop unrealistic observations
winsor2 egdp_12m, cuts(2 98) suffix(_w)
winsor2 egdp_5y, cuts(2 98) suffix(_w)

*** interaction of priors and posteriors
gen egdp_12m_w_X_IT=egdp_12m_w*treatment_IT
gen egdp_12m_w_X_AIT=egdp_12m_w*treatment_AIT

*** probability that inflation is greater than 4 percent (prior)
gen prob_prior=	epi_12m_12plus + epi_12m_8_12 + epi_12m_4_8

*** probability that inflation is greater than 5 percent (posterior)
gen prob_post=QJH18_1

*** winsorize/drop unrealistic observations
winsor2 prob_post, cuts(2 98) suffix(_w)
winsor2 prob_prior, cuts(2 98) suffix(_w)

*** interaction of priors and posteriors
gen prob_prior_w_X_IT=prob_prior_w*treatment_IT
gen prob_prior_w_X_AIT=prob_prior_w*treatment_AIT


*** winsorize/drop unrealistic observations
winsor2 epi_12m, cuts(2 98) suffix(_w)
winsor2 epi_5y, cuts(2 98) suffix(_w)

*** interaction of priors and posteriors
gen epi_12m_w_X_IT=epi_12m_w*treatment_IT
gen epi_12m_w_X_AIT=epi_12m_w*treatment_AIT

gen epi_12m_mean_X_IT=epi_12m_mean*treatment_IT
gen epi_12m_mean_X_AIT=epi_12m_mean*treatment_AIT

*** compute the difference non-parametrically

tempname 1
postfile `1'  ///
	str20 outcome ///
	str10 b_treatment_IT ///
	str10 b_treatment_AIT ///
	str10 pval ///	
	N_reg ///
	str10 R2_reg ///
	using "C:\Dropbox\Average_target_survey\replication_folder\outregs\Table04_RCT_level_OLS_formatted.dta", replace every(1)
	
tempname 2
postfile `2'  ///
	str20 outcome ///
	str10 b_treatment_IT ///
	str10 b_treatment_AIT ///
	str10 pval ///		
	N_reg ///
	str10 R2_reg ///
	powerdiff ///
	using "C:\Dropbox\Average_target_survey\replication_folder\outregs\Table04_RCT_level_HUBER_formatted.dta", replace every(1)
	
*===============================================================================
*			regressions forvariables with priors
*===============================================================================

foreach var in epi_12m_w prob_prior_w egdp_12m_w edy_12m_w {
	
	capture drop POSTERIOR*
	if "`var'"=="epi_12m_w" gen POSTERIOR=epi_5y_w
	if "`var'"=="epi_12m_mean" gen POSTERIOR=epi_5y_w
	if "`var'"=="prob_prior_w" gen POSTERIOR=prob_post_w
	if "`var'"=="egdp_12m_w" gen POSTERIOR=egdp_5y_w
	if "`var'"=="edy_12m_w" gen POSTERIOR=edy_5y_w
	
	capture drop wgtXb2
	
	capture drop _*
	gen _POSTERIOR=POSTERIOR*sqrt(weight)
	gen _treatment_IT=treatment_IT*sqrt(weight)
	gen _treatment_AIT=treatment_AIT*sqrt(weight)
	gen _W=sqrt(weight)
	
	rreg2 _POSTERIOR _treatment_IT _treatment_AIT _W, nolog gen(wgtXb2) 
	replace wgtXb2=wgtXb2*weight
	
	
	*** OLS
	reg POSTERIOR treatment_IT treatment_AIT [aw=weight], robust
	
	foreach vv in treatment_IT treatment_AIT {
	    local b_`vv'=string(_b[`vv'],"%4.3f")
		local se_`vv'="(" + string(_se[`vv'],"%4.3f") + ")"
		
		local p1=_b[`vv']/_se[`vv']
		
		if abs(`p1')>2.62 & `p1'~=. local b_`vv'="`b_`vv''" + "*"
		if abs(`p1')>1.98 & `p1'~=. local b_`vv'="`b_`vv''" + "*"
		if abs(`p1')>1.65 & `p1'~=. local b_`vv'="`b_`vv''" + "*"
	}
	
	test  treatment_IT = treatment_AIT
	local pval=string(r(p),"%4.3f")
	post `1' ("`var'") ("`b_treatment_IT'") ("`b_treatment_AIT'") ("`pval'")   (e(N))  (string(e(r2),"%4.3f") )
	post `1' (" ")   ("`se_treatment_IT'") ("`se_treatment_AIT'") (" ")  (.) (" ")
		
	
	*** HUBER
	reg POSTERIOR treatment_IT treatment_AIT [aw=wgtXb2], robust
	
	
		
	gen POSTERIOR_control=POSTERIOR if treatment_IT==0 & treatment_AIT==0
	gen POSTERIOR_IT=POSTERIOR if treatment_IT==1
	gen POSTERIOR_AIT=POSTERIOR if treatment_AIT==1
	
	sum POSTERIOR_AIT  [aw=wgtXb2]
	local m1=r(mean)
	local sd1=r(sd)
	local N1=r(N)
	
	sum POSTERIOR_IT  [aw=wgtXb2]
	local m2=r(mean)
	local sd2=r(sd)
	local N2=r(N)
	
	power twomeans `m1' `m2', n1(`N1') n2(`N2') sd1(`sd1') sd2(`sd2') 
	
	local m1a=`m1'+0.01
	local m1b=`m1'+1
	local min_dist
	quiet forvalues ix=`m1a'(0.01)`m1b' {
		local ix1=`ix'-`m1'
			power twomeans `m1' `ix', n1(`N1') n2(`N2') sd1(`sd1') sd2(`sd2')
			if r(power)>0.8 {
				local min_distx=`ix1'
				continue, break
			}
			
	}

	noisily di `min_distx'
	

	
	foreach vv in treatment_IT treatment_AIT {
	    local b_`vv'=string(_b[`vv'],"%4.3f")
		local se_`vv'="(" + string(_se[`vv'],"%4.3f") + ")"
		
		local p1=_b[`vv']/_se[`vv']
		
		if abs(`p1')>2.62 & `p1'~=. local b_`vv'="`b_`vv''" + "*"
		if abs(`p1')>1.98 & `p1'~=. local b_`vv'="`b_`vv''" + "*"
		if abs(`p1')>1.65 & `p1'~=. local b_`vv'="`b_`vv''" + "*"
	}
	
	test  treatment_IT = treatment_AIT
	local pval=string(r(p),"%4.3f")
	
	post `2' ("`var'")   ("`b_treatment_IT'") ("`b_treatment_AIT'")  ("`pval'")  (e(N))  (string(e(r2),"%4.3f") ) (`min_distx')
	post `2' (" ")   ("`se_treatment_IT'") ("`se_treatment_AIT'") (" ")   (.) (" ")	(.)
	
}


*===============================================================================
*			regressions forvariables without priors
*===============================================================================

gen MORT=QJH16 if QJH16~=99
gen UNSURE=(QJH16==99)
gen credibility = QJH17_1

foreach var in credibility MORT UNSURE {
	noisily di "`var'"
	
	capture drop POSTERIOR*
	gen POSTERIOR=`var'
	

	capture drop wgtXb2
	
	capture drop _*
	gen _POSTERIOR=POSTERIOR*sqrt(weight)
	gen _treatment_IT=treatment_IT*sqrt(weight)
	gen _treatment_AIT=treatment_AIT*sqrt(weight)
	gen _W=sqrt(weight)
	
	capture {
		rreg2 _POSTERIOR  _treatment_IT _treatment_AIT _W, nolog gen(wgtXb2)
		replace wgtXb2=wgtXb2*weight
	}
	if _rc~=0 {
	    gen wgtXb2=weight
	}
	*** OLS
	reg POSTERIOR treatment_IT treatment_AIT [aw=weight], robust
	
	foreach vv in  treatment_IT treatment_AIT {
	    local b_`vv'=string(_b[`vv'],"%4.3f")
		local se_`vv'="(" + string(_se[`vv'],"%4.3f") + ")"
		
		local p1=_b[`vv']/_se[`vv']
		
		if abs(`p1')>2.62 & `p1'~=. local b_`vv'="`b_`vv''" + "*"
		if abs(`p1')>1.98 & `p1'~=. local b_`vv'="`b_`vv''" + "*"
		if abs(`p1')>1.65 & `p1'~=. local b_`vv'="`b_`vv''" + "*"
	}
	
	test  treatment_IT = treatment_AIT
	local pval=string(r(p),"%4.3f")
	
	post `1' ("`var'")   ("`b_treatment_IT'") ("`b_treatment_AIT'")  ("`pval'")    (e(N)) (string(e(r2),"%4.3f") )
	post `1' (" ")   ("`se_treatment_IT'") ("`se_treatment_AIT'")   (" ")   (.) (" ")
		
	
	*** HUBER
	reg POSTERIOR treatment_IT treatment_AIT  [aw=wgtXb2], robust
	
	gen POSTERIOR_control=POSTERIOR if treatment_IT==0 & treatment_AIT==0
	gen POSTERIOR_IT=POSTERIOR if treatment_IT==1
	gen POSTERIOR_AIT=POSTERIOR if treatment_AIT==1
	
	sum POSTERIOR_AIT  [aw=wgtXb2]
	local m1=r(mean)
	local sd1=r(sd)
	local N1=r(N)
	
	sum POSTERIOR_IT  [aw=wgtXb2]
	local m2=r(mean)
	local sd2=r(sd)
	local N2=r(N)
	
	power twomeans `m1' `m2', n1(`N1') n2(`N2') sd1(`sd1') sd2(`sd2') 
	
	local m1a=`m1'+0.01
	local m1b=`m1'+1
	local min_dist
	quiet forvalues ix=`m1a'(0.01)`m1b' {
		local ix1=`ix'-`m1'
			power twomeans `m1' `ix', n1(`N1') n2(`N2') sd1(`sd1') sd2(`sd2')
			if r(power)>0.8 {
				local min_distx=`ix1'
				continue, break
			}
			
	}

	noisily di `min_distx'
	

	foreach vv in treatment_IT treatment_AIT  {
	    local b_`vv'=string(_b[`vv'],"%4.3f")
		local se_`vv'="(" + string(_se[`vv'],"%4.3f") + ")"
		
		local p1=_b[`vv']/_se[`vv']
		
		if abs(`p1')>2.62 & `p1'~=. local b_`vv'="`b_`vv''" + "*"
		if abs(`p1')>1.98 & `p1'~=. local b_`vv'="`b_`vv''" + "*"
		if abs(`p1')>1.65 & `p1'~=. local b_`vv'="`b_`vv''" + "*"
	}
	
	test  treatment_IT = treatment_AIT
	local pval=string(r(p),"%4.3f")
	
	post `2' ("`var'")   ("`b_treatment_IT'") ("`b_treatment_AIT'")   ("`pval'")   (e(N))  (string(e(r2),"%4.3f") )  (`min_distx')
	post `2' (" ")  ("`se_treatment_IT'") ("`se_treatment_AIT'")   (" ")  (.) (" ")	(.)
	
}



postclose `1'
postclose `2'

